//
//  qiyeCollectionTableViewController.m
//  InkLifeProduct
//
//  Created by 张玉鹏 on 2017/7/9.
//  Copyright © 2017年 马JL. All rights reserved.
//

#import "ShouruTableViewController.h"
#import "JifenTableViewCell.h"

@interface ShouruTableViewController ()
@property(nonatomic,assign)NSInteger page;
@end

@implementation ShouruTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title=@"收支明细";
   [self.tableView registerNib:[UINib nibWithNibName:@"JifenTableViewCell" bundle:nil] forCellReuseIdentifier:@"JifenTableViewCell"];
    self.tableView.rowHeight=80;
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(self.tableView.separatorInset.top, 0, self.tableView.separatorInset.bottom, 0)];

    self.tableView.backgroundColor = ZKcolor(240, 240, 240);
    self.page=1;
    [self getQiyeweilistWithIsRfresh:NO];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mainRefresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(mainLoadMore)];
   
}

-(void)mainRefresh
{
    self.page=1;
    [self getQiyeweilistWithIsRfresh:NO];
}


-(void)mainLoadMore
{
    self.page++;
    [self getQiyeweilistWithIsRfresh:YES];
}


-(void)getQiyeweilistWithIsRfresh:(BOOL)IsRfresh
{
    
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];

    paramets[@"page"] = [NSString stringWithFormat:@"%ld", (long)self.page];
   
    
    [HttpRequest sendSelfGetRequestWithURL:@"api/Credits/GetItem" WithParamets:paramets success:^(id result) {
        
        ZYjifen*jifen1=[ZYjifen mj_objectWithKeyValues:result];
        
        if ([jifen1.code isEqualToString:@"1"]) {
            
            if (IsRfresh==YES) {
                [self.jifen.result addObjectsFromArray:jifen1.result];
            }
            else{
                self.jifen=jifen1;
            }
            
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
        }else{
            [SVProgressHUD showInfoWithStatus:jifen1.msg];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
    }];

    
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jifen.result.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JifenTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"JifenTableViewCell" forIndexPath:indexPath];
    
    ZYjifen*jifen=self.jifen.result[indexPath.row];
    cell.titleLable.text=jifen.Title;
    cell.timeLable.text=jifen.CreateTime;
    cell.jifenlable.text=jifen.ChangeCredit;
    if ([jifen.ChangeCredit integerValue]>0) {
        cell.jifenlable.textColor=[UIColor redColor];
        cell.jifenlable.text=[NSString stringWithFormat:@"+%@",jifen.ChangeCredit];
    }
    else{
         cell.jifenlable.textColor=ZKcolor(47, 198, 107);
    }
   
    return cell;
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
